package main

import (
    _ "sql"
    "database/sql"
    "fmt"
)

func main() {
    db, err := sql.Open("mysql", "root:admin1181@tcp(localhost:3306)/test?charset=utf8")
    checkErr(err)

    //插入数据
    /*stmt, err := db.Prepare("INSERT into authors(email,name) values(?,?)")
    checkErr(err)

    res, err := stmt.Exec("astaxie_tt@gmail.com","astaxie")
    checkErr(err)

    id, err := res.LastInsertId()
    checkErr(err)

    fmt.Println(id)*/
    //更新数据
    
    /*stmt, err = db.Prepare("update authors set name=? where id=?")
    checkErr(err)

    res, err = stmt.Exec("astaxie#", id)
    checkErr(err)

    affect, err := res.RowsAffected()
    checkErr(err)

    fmt.Println(affect)*/

    //查询数据
    rows, err := db.Query("SELECT * FROM authors")
    checkErr(err)

    for rows.Next() {
        var id int
        var email string	
        var name string
        err = rows.Scan(&id, &email, &name)
        checkErr(err)
        fmt.Println(id)
        fmt.Println(email)
        fmt.Println(name)
    }
}

func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}